#!/usr/bin/python
# -*- coding: utf-8 -*- 

import telnetlib
import sys
import time
import sys
from imp import reload

reload(sys)
sys.setdefaultencoding('utf-8')


def excuteCommond(ip, port, line):
    tn = telnetlib.Telnet(ip, port)

    tn.write(line.encode('gb2312'))
    # time.sleep(1)
    print
    tn.read_very_eager()
    print(u"执行完成==" + line)
    tn.close()
    return


if __name__ == '__main__':
    if sys.argv[1] == "help":
        print('\033[1;31;40m')
        print('*' * 20)
        print(u'*执行命令的格式: python dubbo_invoke.py 10.174.106.192 20550 test001.txt')
        print('*' * 20)
        print('\033[0m')

        sys.exit()

    ip = sys.argv[1]
    port = sys.argv[2]
    fileName = sys.argv[3]

    print("ip=" + ip + ", port=" + port + ", fileName=" + fileName)
    f = open(fileName, "r")
countNum = 0
while True:
    line = f.readline()
    if line:
        pass
        print(u"执行开始==" + line)
        excuteCommond(ip, port, line)
        countNum += 1
        print(u"第 " + str(countNum) + " 条, 执行完成")
    else:
        break
# time.sleep(1)
f.close()

print('\033[1;31;40m')
print('*' * 20)
print(u'*总共执行完成 ' + str(countNum) + ' 条.')
print('*' * 20)
print('\033[0m')

# readFile("test003", "20550", "test001.txt")
# readFile("help")
